#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "mythread.h"
#include "mydef.h"
#include <semaphore.h>
#include "main.h"


#ifndef __MYPROC_H__
#define __MYPROC_H__


struct __COMMAND
{
    uint8_t head;//开始头
    uint16_t len;//数据长度
    uint8_t order;//包序号
    uint8_t mode;//模式
    uint8_t dat[128];//数据
    uint8_t checksum; //校验
    //uint8_t OK; //有数据的标志
};

typedef struct __USBHIDKEYMSG
{
    uint8_t head1;
    uint8_t head2;
    uint8_t addr;
    uint8_t command;
    uint8_t len;
    uint8_t dat[8];
    uint8_t sum;
}USBHID;

enum __USBHIDKEYVALUE
{
    USBHIDKEY_0 = 0x27,
    USBHIDKEY_1 = 0x1E,
    USBHIDKEY_2 = 0x1F,
    USBHIDKEY_3 = 0x20,
    USBHIDKEY_4 = 0x21,
    USBHIDKEY_5 = 0x22,
    USBHIDKEY_6 = 0x23,
    USBHIDKEY_7 = 0x24,
    USBHIDKEY_8 = 0x25,
    USBHIDKEY_9 = 0x26,
    USBHIDKEY_CONFIRM = 0x28,
};

enum __KEYVALUES
{
    KEY_0 = 0x09,
    KEY_1 = 0x1F,
    KEY_2 = 0x0F,
    KEY_3 = 0x0E,
    KEY_4 = 0x02,
    KEY_5 = 0x01,
    KEY_6 = 0x10,
    KEY_7 = 0x05,
    KEY_8 = 0x04,
    KEY_9 = 0x03,
    KEY_DOT = 0x13,
    KEY_F1 = 0x0C,
    KEY_F2 = 0x1C,
    KEY_F3 = 0x1D,
    KEY_F4 = 0x1E,
    KEY_WORK = 0x08,
    KEY_MEMBER = 0x07,
    KEY_CANCEL = 0x06,
    KEY_MODIFY = 0x14,
    KEY_DELETE = 0x15,
    KEY_CONFIRM = 0x17,
    KEY_CASH = 0x0D
};

extern struct __COMMAND mapaiCommand;
extern struct __COMMAND  keyCommand;
extern int16_t memberOrder; //成员的数组位置计数
extern uint16_t maxMemberItem; //成员的总数，在主函数中获得
void AnalysisMAPAICommand(uint8_t *pdat);
void AnalysisKEYBOARDCommand(uint8_t *pdat);
void GenerateVersionMsg(void *msg);
void GenerateInitMsg(void *msg);
void GenerateKeyReplyMsg(void *msg);
void GeneratePeriodResponseMsg(void *msg);
void GenerateKeyPressMsg(void *msg, uint8_t key);
void GenerateKeyReleaseMsg(void *msg, uint8_t key);
uint8_t GetKeyOrder(void);
uint16_t GetBillTotalItem(uint8_t *pdat);
double GetBillTotalPrice(uint8_t *pdat);
uint16_t GetBillEan13Code(uint8_t *pdat, BILLINFO *pbill);
uint16_t GetYanFileItem(int8_t *pbuff, off_t size);
YANINFO *ConvertFile2Struc(int8_t *pbuff, off_t size, size_t totalItem);
uint16_t MatchEAN13CODE(BILLINFO *pbill, YANINFO *pyaninfo);
uint16_t TrasferMatchKey(void *msg, int8_t dat);
uint16_t GetMemberItemCount(int8_t *pbuff, off_t file_size);
TEL *GetMemberItemContent(int8_t *pbuff, off_t file_size);
uint16_t GetMemberOrder(void);
void TransferMembers(void *msg, uint16_t order);
uint8_t ConvertASCLL2USBHID(uint8_t keyvalue);
void GenerateUSBHIDKeyPressMsg(void *msg, uint8_t keyvalue);
void GenerateUSBHIDKeyReleaseMsg(void *msg);
void TransferUSBHIDKey(void *msg, uint8_t keyvalue);
void GenerateUSBHIDCmdGetInfoMsg(void *msg);
void GenerateUSBHIDResetMsg(void *msg);
uint8_t IsReturnBill(uint8_t *pdat);
void SetMemberOrderForward(void);
void SetMemberOrderBack(void);
#endif